package com.shangxian.giveithome.controller;


import com.shangxian.giveithome.annoaction.CheckLoginStatus;
import com.shangxian.giveithome.code.ActivityCode;
import com.shangxian.giveithome.code.MechanismCode;
import com.shangxian.giveithome.entity.ActivityInfo;
import com.shangxian.giveithome.entity.ActivityScreen;
import com.shangxian.giveithome.entity.ActivityUpdate;
import com.shangxian.giveithome.entity.UserDetails;
import com.shangxian.giveithome.exception.ExceptionUtils;
import com.shangxian.giveithome.result.Result;
import com.shangxian.giveithome.service.ActivityService;
import com.shangxian.giveithome.utils.RedisUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@Api(tags = "活动模块")
@RestController
@RequestMapping("/activity")
public class ActivityController {

  @Autowired
  private ActivityService activityService;

  @Autowired
  private RedisUtils redisUtils;

  @Autowired
  private HttpServletRequest request;

  @GetMapping("/queryByActivityId")
  @ApiOperation(value = "根据机构id查询活动列表")
  public Result queryByActivityId(@Validated ActivityScreen activityScreen, @PageableDefault(page = 0, size = 10) Pageable pageable) {
    Long aLong = redisUtils.getMechanismCheck(request);
    if (!activityScreen.getId().equals(aLong)) {
      ExceptionUtils.error(ActivityCode.USER_NOT_AUTH);
    }
    return Result.success(activityService.queryByActivityId(activityScreen, pageable));
  }

  @ApiOperation(value = "查看机构正在进行中的活动前n条")
  @GetMapping("/queryActivityIng/{id}/{n}")
  public Result queryByActivityIng(@PathVariable Long id, @PathVariable Integer n) {
    return Result.success(activityService.queryByActivityIng(id, n));
  }

  @ApiOperation(value = "根据id查询活动详情 用户")
  @GetMapping("/queryActivityUserDetails/{id}")
  public Result queryActivityDetails(@PathVariable Long id) {
    return Result.success(activityService.queryByActivity(id));

  }

  @ApiOperation(value = "根据id查询活动详情 机构")
  @GetMapping("/queryActivityMeDetails/{id}")
  public Result queryActivityMeDetails(@PathVariable Long id) {
    redisUtils.checkStatus(request);
    //机构查看活动详情
    return Result.success(activityService.queryByActivityMechanism(id));
  }

  @ApiOperation(value = "查看活动人员数据")
  @GetMapping("/queryActivityMail/{id}")
  @CheckLoginStatus
  public Result queryActivityMail(@PathVariable Long id, @PageableDefault(page = 0, size = 10) Pageable pageable) {
    Long mechanismCheck = redisUtils.getMechanismCheck(request);
    if (mechanismCheck == -1L) {
      ExceptionUtils.error(ActivityCode.USER_NOT_MAIL_AUTH);
    }
    return activityService.queryActivityMail(id, pageable);
  }

  @ApiOperation(value = "创建活动")
  @PostMapping("/createActivity")
  public Result createActivity(@RequestBody @Validated ActivityInfo activityInfo) {
    Long aLong = redisUtils.getMechanismCheck(request);
    if (aLong == -1L) {
      ExceptionUtils.error(MechanismCode.USER_NOT_U_AUTH);
    }
    activityInfo.setMechanismId(aLong);
    activityService.create(activityInfo);
    return Result.success();
  }

  @ApiOperation(value = "修改活动")
  @PutMapping("/updateActivity")
  public Result update(@RequestBody @Validated ActivityUpdate activityUpdate) {
    Long aLong = redisUtils.getMechanismCheck(request);
    if (aLong == -1L) {
      ExceptionUtils.error(MechanismCode.USER_NOT_U_AUTH);
    }
    activityUpdate.setMechanismId(aLong);
    activityService.update(activityUpdate);
    return Result.success();
  }

  @ApiOperation(value = "删除活动")
  @DeleteMapping("/deleteActivity")
  public Result deleteActivity(Long id) {
    Long aLong = redisUtils.getMechanismCheck(request);
    if (aLong == -1L) {
      ExceptionUtils.error(MechanismCode.USER_NOT_D_AUTH);
    }
    activityService.delete(id, aLong);
    return Result.success();
  }

  @ApiOperation(value = "报名活动")
  @PostMapping("/signActivity")
  public Result signActivity(Long id) {
    UserDetails header = redisUtils.getHeader(request);
    activityService.singActivityUser(id, header.getId());
    return Result.success();
  }

  @ApiOperation(value = "查看活动二维码")
  @GetMapping("/queryRq")
  public Result queryRq(Long acId) {
    Long aLong = redisUtils.getMechanismCheck(request);
    if (aLong == -1L) {
      ExceptionUtils.error(MechanismCode.USER_NOT_S_AUTH);
    }
    return Result.success(activityService.queryRq(acId));
  }

  @ApiOperation(value = "签到")
  @PostMapping("/SignActivity")
  public Result SignActivity(@RequestParam("ac_id") Long ac_id, @RequestParam("me_id") Long me_id) {
    UserDetails header = redisUtils.getHeader(request);
    activityService.SignActivity(ac_id, me_id, header.getId());
    return Result.success();
  }
}


